package com.thoughtworks.impersonator.http.record;


import static org.jboss.netty.channel.Channels.pipeline;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;

import com.thoughtworks.impersonator.http.message.storage.HttpExchangeStorage;

public class RecorderPipelineFactory implements ChannelPipelineFactory {

    private ClientBootstrap clientBootstrap;
    private String remoteHost;
    private int remotePort;
    private final HttpExchangeStorage storage;

    public RecorderPipelineFactory(ClientBootstrap clientBootstrap, HttpExchangeStorage storage, String remoteHost, int remotePort) {
        this.clientBootstrap = clientBootstrap;
        this.storage = storage;
        this.remoteHost = remoteHost;
        this.remotePort = remotePort;
    }

    @Override
    public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pipeline = pipeline();
        pipeline.addLast("decoder", new HttpRequestDecoder());
        pipeline.addLast("chunkAggregator", new HttpChunkAggregator(1048576));
        pipeline.addLast("proxy", new ClientConnectionHandler(clientBootstrap, storage, remoteHost, remotePort));
        pipeline.addLast("encoder", new HttpResponseEncoder());
        return pipeline;
    }

}
